package com.example.easybuy.controller;

import com.antherd.smcrypto.sm3.Sm3;
import com.example.easybuy.annotaion.CheckPermission;
import com.example.easybuy.entity.User;
import com.example.easybuy.service.UserService;
import com.example.easybuy.util.AjaxResult;
import com.example.easybuy.util.JwtUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping("/findUserFromRedis")
    @CheckPermission({"0","1"})
    public AjaxResult findUserFromRedis(@RequestHeader(name="Authorization") String token) {
        String loginName= JwtUtil.getLoginName(token);
        User user=userService.findUser(loginName);
        return AjaxResult.success().add("user", user);
    }

    @RequestMapping("/getList")
    @CheckPermission({"1"})
    public AjaxResult byPage(@RequestParam Map<String, Object> map) {
        return AjaxResult.success().add("pageInfo", userService.userByPage(map));
    }

    @RequestMapping("/isFind")
    public AjaxResult isFind(String loginName) {
        User user = userService.findUser(loginName);
        return AjaxResult.success().add("user", user);
    }


    @PostMapping("/register")
    public AjaxResult register(@RequestBody Map<String, Object> map) {
        String password = map.get("password").toString();
        String md5Password = Sm3.sm3(password);
        map.put("password", md5Password);

        return AjaxResult.success().add("status", userService.register(map));
    }

    @RequestMapping("/delUser")
    @CheckPermission("1")
    public AjaxResult delUser(int id) {
        return AjaxResult.success().add("status", userService.delUser(id));
    }

    @RequestMapping("/updateUser")
    @CheckPermission({"1","0"})
    public AjaxResult updateUser(@RequestParam Map<String, Object> map) {

        if (map.containsKey("password") && !map.get("password").toString().isEmpty()) {
            String password = map.get("password").toString();
            String md5Password = Sm3.sm3(password);
            map.put("password", md5Password);
        }else {
            map.remove("password");
        }
        return AjaxResult.success().add("status", userService.updateUser(map));
    }

    @RequestMapping("/exit")
    @CheckPermission({"0","1"})
    public AjaxResult exit(@RequestHeader(name="Authorization") String token){
        String loginName= JwtUtil.getLoginName(token);
        if (Boolean.TRUE.equals(stringRedisTemplate.delete(loginName))){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @RequestMapping("/equalPassword")
    @CheckPermission({"0","1"})
    public AjaxResult equalPassword(String password,String inputPassword){
        String sm3Password=Sm3.sm3(inputPassword);
        if (sm3Password.equals(password)){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }
}